package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysProject;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysProjectService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 项目管理Controller
 * 
 * @author cooc
 * @date 2024-06-19
 */

@Api(tags = "项目管理")
@RestController
@RequestMapping("/system/project")
public class SysProjectController extends BaseController
{
    @Autowired
    private ISysProjectService sysProjectService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询项目管理列表
     */
    @ApiOperation("查询项目管理列表")
    @ApiResponse(code = 200, message = "查询成功", response = SysProject.class)
    @PreAuthorize("@ss.hasPermi('system:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProject sysProject)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(Convert.toInt(ServletUtils.getParameter(TableSupport.PAGE_NUM), 1));
        pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameter(TableSupport.PAGE_SIZE), 10));
        pageDomain.setOrderByColumn("create_time");
        pageDomain.setIsAsc("desc");
        startPage(pageDomain);
        boolean role = SecurityUtils.hasRole("@systemAdmin");
        if(role){
            return  getDataTable(sysProjectService.selectSysProjectList(sysProject));
        }
        return getDataTable(sysProjectService.selectSysProjectList(sysProject,getUserId()));
    }


    /**
     * 查询项目授权人员列表
     */
    @ApiOperation("查询项目授权人员列表")
    @ApiResponse(code = 200, message = "查询成功", response = SysUser.class)
    @PreAuthorize("@ss.hasPermi('system:project:list')")
    @GetMapping("/getAuthorList")
    public TableDataInfo getAuthorList()
    {
        Long id1 = sysRoleMapper.selectRoleByRoleKey("@platform").getRoleId();
        Long id2 =sysRoleMapper.selectRoleByRoleKey("@employee").getRoleId();
        List<Long> list1 = sysUserRoleMapper.selectUserRoleByRoleId(id1).stream().map(SysUserRole::getUserId).collect(Collectors.toList());
        list1.addAll(sysUserRoleMapper.selectUserRoleByRoleId(id2).stream().map(SysUserRole::getUserId).collect(Collectors.toList()));
        List<SysUser> userList = sysUserService.selectUserList(new SysUser());
        List<SysUser> res =  userList.stream().filter(u -> list1.contains(u.getUserId())).collect(Collectors.toList());
        startPage();
        return getDataTable(res);
    }

    /**
     * 导出项目管理列表
     */
    @ApiOperation("导出项目管理列表")
    @ApiResponse(code = 200, message = "导出成功", response = SysProject.class)
    @PreAuthorize("@ss.hasPermi('system:project:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProject sysProject)
    {
        List<SysProject> list = sysProjectService.selectSysProjectList(sysProject);
        ExcelUtil<SysProject> util = new ExcelUtil<SysProject>(SysProject.class);
        util.exportExcel(response, list, "项目管理数据");
    }

    /**
     * 获取项目详细信息
     */

    @ApiOperation("获取项目详细信息")
    @ApiResponse(code = 200, message = "获取成功", response = SysProject.class)
    @PreAuthorize("@ss.hasPermi('system:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysProjectService.selectSysProjectById(id));
    }

    /**
     * 新增项目
     */
    @ApiOperation("新增项目")
    @ApiResponse(code = 200, message = "新增成功", response = SysProject.class)
    @PreAuthorize("@ss.hasPermi('system:project:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProject sysProject)
    {
        return toAjax(sysProjectService.insertSysProject(sysProject));
    }

    /**
     * 维护项目自定义字段
     */
    @ApiOperation("维护项目自定义字段")
    @ApiResponse(code = 200, message = "新增成功", response = SysProject.class)
    @PreAuthorize("@ss.hasPermi('system:project:add')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/custom")
    public AjaxResult custom (@RequestBody SysProject customJson)
    {
        SysProject obj = sysProjectService.selectSysProjectById(customJson.getId());
        obj.setProjectInfo(customJson.getProjectInfo());
        return toAjax(sysProjectService.updateSysProjectInfo(obj));
    }

    /**
     * 修改项目
     */
    @ApiOperation("修改项目")
    @ApiResponse(code = 200, message = "修改成功", response = SysProject.class)
    @PreAuthorize("@ss.hasPermi('system:project:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProject sysProject)
    {
        return toAjax(sysProjectService.updateSysProject(sysProject));
    }

    /**
     * 删除项目
     */
    @ApiOperation("删除项目")
    @ApiResponse(code = 200, message = "删除成功", response = SysProject.class)
    @PreAuthorize("@ss.hasPermi('system:project:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysProjectService.deleteSysProjectByIds(ids));
    }



}
